from django import http
from django.core.cache import cache
from django.shortcuts import render

# Create your views here.
from django.views import View
from areas.models import Area
from meixixi.utils.response_code import RETCODE


class AreaView(View):
    def get(self,request):
        area_id = request.GET.get('area_id')

        if area_id:
            subs = cache.get("subs_%s" % area_id)

            if subs:
                context = {
                    "code": RETCODE.OK,
                    "sub_data": {
                        "subs": subs
                    }
                }
                return http.JsonResponse(context)

            area = Area.objects.get(id=area_id)
            cities = area.subs.all()

            subs_list = []
            for city in cities:
                city_dict = {
                    'id':city.id,
                    'name':city.name
                }
                subs_list.append(city.name)
            context = {
                    'code':RETCODE.OK,
                    'sub_data':{
                        'subs':subs_list
                    }
                }
            cache.set('subs_%s'%area_id,subs_list)
            return http.JsonResponse(context)

        else:
            # TODO 查询缓存,如果有直接返回
            province_list = cache.get("province_list")

            if province_list:
                return http.JsonResponse({
                    "code": RETCODE.OK,
                    "province_list": province_list
                })

            # 3,查询所有省的数据
            provinces = Area.objects.filter(parent=None).all()

            # 3,1拼接数据
            province_list = []
            for province in provinces:
                province_dict = {
                    "id": province.id,
                    "name": province.name
                }
                province_list.append(province_dict)

            context = {
                "code": RETCODE.OK,
                "province_list": province_list
            }

            # TODO 缓存省的数据
            cache.set("province_list", province_list)

            # 3,2返回响应
            return http.JsonResponse(context)